package utils;

import entity.Class;
import entity.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class AddDataUtil {
    public static int addData(Integer id,String name,Integer age,String iphone) throws SQLException {
        String url= "jdbc:mysql://localhost:3306/db01";
        String user = "root";
        String password="123321";
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
        String sql = "insert into user(id,name,age,iphone) values(?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        preparedStatement.setString(2,name);
        preparedStatement.setInt(3,age);
        preparedStatement.setString(4,iphone);
        int i = preparedStatement.executeUpdate();
        connection.close();
        return i;
    }
    public static int insertData(User username) throws SQLException {
        String url= "jdbc:mysql://localhost:3306/db01";
        String user = "root";
        String password="123321";
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
        String sql = "insert into user(id,name,age,iphone) values(?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,username.getId());
        preparedStatement.setString(2,username.getName());
        preparedStatement.setInt(3,username.getAge());
        preparedStatement.setString(4,username.getIphone());
        int i = preparedStatement.executeUpdate();
        connection.close();
        return i;
    }
    public static int insertParam(String sql, List list) throws SQLException {
        String url= "jdbc:mysql://localhost:3306/db01";
        String user = "root";
        String password="123321";
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < list.size(); i++) {
            preparedStatement.setObject(i+1,list.get(i));
        }
        int i = preparedStatement.executeUpdate();
        connection.close();
        return i;
    }
    public static int insertParamByObject(String sql, Object...args) throws SQLException {
        String url= "jdbc:mysql://localhost:3306/db01";
        String user = "root";
        String password="123321";
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < args.length; i++) {
            preparedStatement.setObject(i+1,args[i]);
        }
        int i = preparedStatement.executeUpdate();
        connection.close();
        return i;
    }
    public static int insertClass(Class clazz) throws SQLException {
        String url= "jdbc:mysql://localhost:3306/db01";
        String user = "root";
        String password="123321";
        Connection connection = DriverManager.getConnection(url,user,password);
        System.out.println(connection);
        String sql = "insert into cs_2023(id,name,banji) values(?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,clazz.getId());
        preparedStatement.setString(2,clazz.getName());
        preparedStatement.setString(3,clazz.getClazz());
        int i = preparedStatement.executeUpdate();
        connection.close();
        return i;
    }
}
